﻿
@{
    ViewBag.Title = "Index";
}
<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=tS3FyA4fEjvsT8Khwfl2LbBHeMuxjHTc"></script>
<div class="panel-body" style="padding-bottom:0px;">
    <div class="panel panel-default">
        <div class="panel-heading">查询条件</div>
        <div class="panel-body">
            <form id="formSearch" class="form-horizontal">
                <div class="form-group" style="margin-top:15px">
                    <label class="control-label col-sm-1" for="txt_search_departmentname">地址</label>
                    <div class="col-sm-2">
                        <input type="text" class="form-control txtAddress" id="txtAddress" autocomplete="off">
                    </div>
                    <div class="col-sm-4">
                        <label class="form-inline" for="selectedPrinter">几室几厅(厨卫)：</label>
                        <select name="Detailed" id="Detailed" class="form-control " style="width:180px;display:inline">
                        </select>
                    </div>
                    <div class="col-sm-2" style="text-align:left;">
                        <button type="button" style="margin-left:50px" id="btn_query" onclick="GetQuery()" class="btn btn-primary">查询</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
    <div id="toolbar" class="btn-group">
        <input type="hidden" id="index" value="" />
        <button id="btn_add" type="button" class="btn btn-default" data-toggle="modal" data-target="#modal">
            <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
        </button>
        <button id="btn_edit" type="button" class="btn btn-default" onclick="up()">
            <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
        </button>
        <button id="btn_delete" type="button" class="btn btn-default" onclick="del()">
            <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
        </button>
    </div>
    <table id="tb1"></table>

    <!-- 添加   修改（Modal） -->
    <div class="modal inmodal fade" id="modal" tabindex="-1" role="dialog" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                    <h4 class="modal-title" id="myModalLabel">添加修改</h4>
                </div>
                <div class="modal-body" style="height:300px">
                    <form class="form-horizontal" role="form">
                        <div>
                            <div class="form-group col-md-6" style="display: inline-block;">
                                <label for="firstname" class="col-sm-4 control-label">位置:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" autocomplete="off" id="Address" placeholder="">
                                </div>
                            </div>
                            <div class="form-group" style="display:inline-block;margin-left:20px;width:50%;">
                                <label for="lastname" class="col-sm-3 control-label">单元房号:</label>
                                <div class="col-sm-9">
                                    <label>
                                        <input type="text" id="Floore_1" autocomplete="off" class="form-control " style="width:50px;display:inline-block" placeholder="1">单元
                                        <input type="text" id="Floore_2" autocomplete="off" class="form-control " style="width:50px;display:inline-block" placeholder="1">号楼
                                        <input type="text" id="Floore_3" autocomplete="off" class="form-control " style="width:50px;display:inline-block" placeholder="1">房间号
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div>
                            <div class="form-group col-sm-6 " style="display: inline-block;">
                                <label for="firstname" class="col-sm-4 control-label">租房类型:</label>
                                <div class="col-sm-8">
                                    <input type="radio" name="type" value="1" checked="">整租
                                    <input type="radio" name="type" value="2">合租
                                </div>
                            </div>
                            <div class="form-group" style="display:inline-block;margin-left:20px;width:50%">
                                <label for="lastname" class="col-sm-3 control-label">配置:</label>
                                <div class="col-sm-9">
                                    <label>
                                        <input type="text" id="Detailed_1" autocomplete="off" class="form-control " style="width:50px;display:inline-block" placeholder="1">室
                                        <input type="text" id="Detailed_2" autocomplete="off" class="form-control " style="width:50px;display:inline-block" placeholder="1">厅
                                        <input type="text" id="Detailed_3" autocomplete="off" class="form-control " style="width:50px;display:inline-block" placeholder="1">厨
                                        <input type="text" id="Detailed_4" autocomplete="off" class="form-control " style="width:50px;display:inline-block" placeholder="1">卫
                                    </label>
                                </div>
                            </div>
                        </div>
                        <div style="">
                            <div class="form-group  col-md-6" style="display: inline-block; vertical-align: top; ">
                                <label for="firstname" class="col-sm-4 control-label">租金:</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control" autocomplete="off" id="HouseMoney" placeholder="">
                                </div>
                            </div>
                            <div class="form-group" style="display:inline-block;margin-left:20px;width:50%">
                                <label for="lastname" class="col-sm-3 control-label">备注:</label>
                                <div class="col-sm-9">
                                    <textarea class="form-control" id="Remark" rows="3"></textarea>
                                </div>
                            </div>
                        </div>
                        <div>
                            <div class="form-group col-sm-6 " style="display: inline-block;">
                                <label for="firstname" class="col-sm-4 control-label">租房时间:</label>
                                <div class="col-sm-8">
                                    <input type="radio" name="HouseLen" value="1" checked="">长租
                                    <input type="radio" name="HouseLen" value="0">短租
                                </div>
                            </div>
                        </div>
                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                    <button type="button" class="btn btn-primary" onclick="btnModal()">保存更改</button>
                </div>
            </div>
        </div>
    </div>
</div>

@section Scripts{
    <script src="~/Content/bootstraptable/dist/bootstrap-table.js"></script>
    <link href="~/Content/bootstraptable/dist/bootstrap-table.css" rel="stylesheet" />
    <script src="~/Content/bootstraptable/dist/locale/bootstrap-table-zh-CN.js"></script>
    <link href="~/Content/layer/layer/mobile/need/layer.css" rel="stylesheet" />
    <script src="~/Content/layer/layer/layer.js"></script>

    <link href="~/Content/Version/css/plugins/iCheck/custom.css" rel="stylesheet">
    <script src="~/Content/Version/js/plugins/pace/pace.min.js"></script>
    <script src="~/Content/Version/js/plugins/iCheck/icheck.min.js"></script>
    <script src="~/Content/Version/js/plugins/typehead/bootstrap3-typeahead.min.js"></script>
    <script>
        $(function () {
            ShowTable();
            DetailedMoHo();
            loadMapAutocomplete("Address");
        });
        //modal地址自动补全
        function loadMapAutocomplete(mySuggestId) {
            Ac = new BMap.Autocomplete( //建立一个自动完成的对象
                {
                    "input": mySuggestId,
                });
        }
        $("#btn_add").click(function () {
            $("#index").val("");
            $("#name").val("");
        })
        //查询展示数据
        function ShowTable() {
            $('#tb1').bootstrapTable({
                url: '/House/HouseSelect',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                toolbar: '#toolbar',                //工具按钮用哪个容器
                striped: true,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: true,                   //是否显示分页（*）
                sortable: true,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: function (params) {
                    params.address = $("#txtAddress").val();
                    params.detailed = $("#Detailed").find("option:selected").text();
                    return params;
                },
                //传递参数（*）*/
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 10,                       //每页的记录行数（*）
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: true,                  //是否显示所有的列
                showRefresh: false,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: true,                //是否启用点击选中行
                // height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "Id",                     //每一行的唯一标识，一般为主键列
                showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: true,                   //是否显示父子表
                columns: [{
                    checkbox: true
                }, {
                    field: 'Address',
                    sortable: true,
                    title: '地址'
                }, {
                    field: 'LandlordName',
                    title: '房主'
                }, {
                    field: 'Floore',
                    sortable: true,
                    title: '楼层房号'
                }, {
                    field: 'Detailed',
                    sortable: true,
                    title: '室厅厨卫'
                }, {
                    field: 'HouseType',
                    title: '租房类型',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "整租";
                        } else if (value == 2) {
                            return "合租";
                        }
                    }
                }, {
                    field: 'HouseMoney',
                    title: '租金'
                }, {
                    field: 'HouseLen',
                    title: '长租短租',
                    formatter: function (value, row, index) {
                        if (value == 1) {
                            return "长租";
                        } else if (value == 0) {
                            return "短租";
                        }
                    }
                }, {
                    field: 'PeopleCount',
                    title: '剩余房间数'
                }, {
                    field: 'Remark',
                    title: '备注'
                    },{
                    field: 'IsFinal',
                        sortable: true,
                    title: '状态',
                         formatter: function (value, row, index) {
                        if (value == 1) {
                            return "上架中";
                        } else if (value == 0) {
                            return "下架中";
                        }
                    }
                    },{
                    title: '操作',
                    field: 'id',
                    align: 'center',
                    events: operateEvents,
                    formatter: operateFormatter
                }
                ],
                onExpandRow: function (index, row, $detail) {
                    createSubTable(index, row, $detail);
                }
            });

        }
        window.operateEvents = {
            //'click #shelves': function (e, value, row, index) {
            //    alert(row.Id);
            //},
            'click #under': function (e, value, row, index) {
                layer.msg('是否确定下架？', {
                    time: 0 //不自动关闭
                    , btn: ['确定', '取消']
                    , yes: function (index) {
                        layer.close(index);
                        layer.prompt({ title: '下架原因', formType: 2 }, function (text, index) {
                            layer.close(index);
                            $.ajax({
                                url: "/House/HouseXiaJia",
                                data: {
                                    text: text,
                                    id:row.Id
                                },
                                success: function (res) {
                                    if (res == "OK") {
                                        layer.msg("下架成功");
                                        SelectIndex();
                                    }
                                    else {
                                        layer.msg("下架失败");
                                    }
                                }
                            })
                        });

                    }
                });
            }
        }
        function operateFormatter(value, row, index) {
            var str = "";
            @*if ("@ViewBag.role" == "H1001" && row.IsFinal=="1") {
                str = '<button type="button" class="shelves btn btn-danger" id="shelves" style="margin-right:15px;">上架</button>';
            }
            else {*@
             if ( row.IsFinal=="1") {
                str = '<button type="button" class="under btn btn-danger" id="under" style="margin-right:15px;">下架</button>';
            }
            return [
                str
            ].join('');


        }

        createSubTable = function (index, row, $detail) {
            var Id = JSON.stringify(row.Id);
            var childId = "tbChild_" + index;
            var cru_table = $detail.html("<table id=" + childId + "></table>").find('table');
            ChildTable(childId, Id);
            $(cru_table).addClass("table table-striped");
        }
        function ChildTable(childId, houseId) {
            $("#" + childId).bootstrapTable({
                url: '/House/TableChild',         //请求后台的URL（*）
                method: 'get',                      //请求方式（*）
                /*             toolbar: '#toolbar',                //工具按钮用哪个容器*/
                striped: false,                      //是否显示行间隔色
                cache: false,                       //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
                pagination: false,                   //是否显示分页（*）
                sortable: false,                     //是否启用排序
                sortOrder: "asc",                   //排序方式
                queryParams: function (params) {
                    params.houseId = houseId;
                    return params;
                },
                //传递参数（*）*/
                sidePagination: "server",           //分页方式：client客户端分页，server服务端分页（*）
                pageNumber: 1,                       //初始化加载第一页，默认第一页
                pageSize: 10,                       //每页的记录行数（*）
                pageList: [10, 25, 50, 100],        //可供选择的每页的行数（*）
                search: false,                       //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
                strictSearch: true,
                showColumns: false,                  //是否显示所有的列
                showRefresh: false,                  //是否显示刷新按钮
                minimumCountColumns: 2,             //最少允许的列数
                clickToSelect: false,                //是否启用点击选中行
                // height: 500,                        //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
                uniqueId: "Id",                     //每一行的唯一标识，一般为主键列
                showToggle: false,                    //是否显示详细视图和列表视图的切换按钮
                cardView: false,                    //是否显示详细视图
                detailView: false,                   //是否显示父子表
                columns: [{
                    field: 'landlordName',
                    title: '租客姓名'

                }, {
                        field: 'Tel',
                        title: '联系方式'
                    }, {
                    field: 'CreateTime',
                    title: '入租时间',
                    formatter: function (value, row, index) {
                        return changeDateFormat(value)
                    }

                }, {
                    field: 'EndTime',
                    title: '到租时间',
                    formatter: function (value, row, index) {
                        return changeDateFormat(value)
                    }

                }],
                onExpandRow: function (index, row, $detail) {
                    createSubTable(index, row, $detail);
                }
            });
        }
        //查询条件
        function GetQuery() {
            // 模糊查询                        查询
            $('#tb1').bootstrapTable('refresh', {
            });
        }

        //modal清空
        function Modalempty() {
            $("#Address").val("");
            $("input[name='type']:checked").val("0");
            $("input[name='HouseLen']:checked").val("0")
            $("#Remark").val("")
            $("#HouseMoney").val("");
            $("#Detailed_1").val("");
            $("#Detailed_2").val("");
            $("#Detailed_3").val("");
            $("#Detailed_4").val("");
            $("#Floore_1").val("");
            $("#Floore_2").val("");
            $("#Floore_3").val("");
        }

        //添加修改
        function btnModal() {
            var address = $("#Address").val();
            if (address == "") {
                layer.msg("地址不为空");
                return;
            }
            var Floore = StringFloore();
            var Detailed = StringDetailed();
            if (Floore == "") {
                layer.msg("楼层房号不为空");
                return;
            }
            var Type = $("input[name='type']:checked").val();
            var HouseLen = $("input[name='HouseLen']:checked").val();
            var Remark = $("#Remark").val();
            var HouseMoney = $("#HouseMoney").val();
            if (isNaN(HouseMoney)) {
                layer.msg("租金只能为数字");
                return;
            }
            $.ajax({
                url: "/House/HouseAdd",
                data: {
                    address: address,
                    Floore: Floore,
                    Detailed: Detailed,
                    Type: Type,
                    Remark: Remark,
                    HouseMoney: HouseMoney,
                    HouseLen: HouseLen,
                    index: $("#index").val(),
                },
                success: function (res) {
                    if (res == "OK") {
                        layer.msg("操作成功");
                        $('#modal').modal('hide');
                        Modalempty();
                        SelectIndex();
                    }
                    else {
                        layer.msg("操作失败");
                    }
                }
            })

        }

        //几室几厅连接拼接字符串
        function StringDetailed() {
            var Detailed = "";
            if ($("#Detailed_1").val() != "") {
                Detailed += $("#Detailed_1").val() + "室"
            }
            else {
                Detailed += "0" + ","
            }
            if ($("#Detailed_2").val() != "") {
                Detailed += $("#Detailed_2").val() + "厅"
            }
            else {
                Detailed += "0" + ","
            }
            if ($("#Detailed_3").val() != "") {
                Detailed += $("#Detailed_3").val() + "厨"
            }
            else {
                Detailed += "0" + ","
            }
            if ($("#Detailed_4").val() != "") {
                Detailed += $("#Detailed_4").val() + "卫"
            }
            else {
                Detailed += "0" + ","
            }
            return Detailed;
        }
        //楼层房号连接拼接字符串
        function StringFloore() {
            var Floore = "";
            var m = 0;
            if ($("#Floore_1").val() != "") {
                Floore += $("#Floore_1").val() + "单元"
            }
            else {
                m += 1;
            }
            if ($("#Floore_2").val() != "") {
                Floore += $("#Floore_2").val() + "号楼"
            }
            else {
                m += 1;
            }
            if ($("#Floore_3").val() != "") {
                Floore += $("#Floore_3").val() + "号房"
            }
            else {
                m += 1;
            }
            if (m != 0) {
                return "";
            }
            else {
                return Floore;
            }
        }
        //修改——转换日期格式(时间戳转换为datetime格式)
        function changeDateFormat(cellval) {
            var dateVal = cellval + "";
            if (cellval != null) {
                var date = new Date(parseInt(dateVal.replace("/Date(", "").replace(")/", ""), 10));
                var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
                var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();

                //var hours = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
                //var minutes = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
                //var seconds = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();

                return date.getFullYear() + "-" + month + "-" + currentDate /*+ " " + hours + ":" + minutes + ":" + seconds*/;
            }
        }
        //删除
        function del() {
            var data = $("#tb1").bootstrapTable('getSelections', function (row) {
                return row;
            })
            if (data == "") {
                layer.msg("请选择");
                return false;
            }
            var str = "";
            for (var i = 0; i < data.length; i++) {
                str += (data[i].Id) + ",";
            }
            layer.msg('是否删除？', {
                time: 0 //不自动关闭
                , btn: ['确定', '取消']
                , yes: function (index) {
                    layer.close(index);
                    $.ajax({
                        url: "/House/HouseDel",
                        data: { lists: str },
                        success: function (res) {
                            if (res == "OK") {
                                layer.msg("删除成功");
                                SelectIndex();
                            }
                            else {
                                layer.msg("删除失败");
                            }
                        }
                    })
                }
            });
        }
        function up() {
            var data = $("#tb1").bootstrapTable('getSelections', function (row) {
                return row;
            })

            if (data == "") {
                layer.msg("请选择");
                return false;
            } else if (data.length != 1) {
                layer.msg("只能选择一个");
                return false;
            }
            $("#btn_add").click();
            $("#index").val(data[0].Id);
            if ($("#index").val() != "") {
                $("#Address").val(data[0].Address);
                $("#Floore_1").val(data[0].Floore.charAt(0));
                $("#Floore_2").val(data[0].Floore.charAt(3));
                $("#Floore_3").val(data[0].Floore.charAt(6));
                $("input [name='type']").val(data[0].type);
                $("#Detailed_1").val(data[0].Detailed.charAt(0));
                $("#Detailed_2").val(data[0].Detailed.charAt(2));
                $("#Detailed_3").val(data[0].Detailed.charAt(4));
                $("#Detailed_4").val(data[0].Detailed.charAt(6));
                $("#HouseMoney").val(data[0].HouseMoney);
                $("#Remark").val(data[0].Remark);


            }
        }
        $('#txtAddress').typeahead({
            source: function (query, process) {
                return $.ajax({
                    url: "/House/AddressJson",
                    data: { name: query },
                    success: function (res) {
                        return process(res);
                    }
                })
            }
        });
        //几室几厅
        function DetailedMoHo() {

            $.ajax({
                url: "/House/DetailedJson",
                data: {},
                success: function (res) {
                    $("#Detailed").html("<option value=\"\" selected=\"\">全部</option>");
                    for (var i = 0; i < res.length; i++) {
                        $("#Detailed").append("<option value=\"" + res[i].Id + "\" >" + res[i].Detailed + "</option>")
                    }
                }
            })
        }
        //控件重新绑定数据
        function SelectIndex() {
            GetQuery();
            DetailedMoHo();
        }
    </script>
    <style>
        #myModal {
            height: 400px;
            /*  width:600px;*/
            top: auto;
        }

        .tangram-suggestion {
            z-index: 9999;
        }
    </style>

}
